from django.db.models import Q
from django.shortcuts import render,redirect,HttpResponse
from django.urls import reverse

from .models import *
from django.views import View
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator

# Create your views here.

class Subject_add(View):
    def get(self,request):
        data=subject.objects.all()
        return render(request,'subject_add.html',{'data':data})
    def post(self,request):
        subName=request.POST.get('subName')
        dat=subject.objects.create(subName=subName)
        return redirect('kaoshi:index')
class Index(View):
    def get(self,request,page=1):
        data=result.objects.all()
        pager=Paginator(data,3)
        pagee=pager.get_page(page)
        return render(request,'index.html',locals())
class Rseult_add(View):
    def get(self,request):
        data=subject.objects.all()
        return render(request,'result_add.html',{'data':data})
    def post(self,request):
        stuName=request.POST.get('stuName')
        grade=request.POST.get('grade')
        sub=request.POST.get('sub')
        subject.objects.create(stuName=stuName,grade=grade,sub_id=sub)
        return redirect('kaoshi:index')
def sever(request,page=1):
    grade=request.GET.get('grade')
    q=Q(grade__contains=grade)
    data=result.objects.filter(q)
    pager = Paginator(data,3)
    pagee = pager.get_page(page)
    return render(request, 'base.html', locals())
class Result_del(View):
    def get(self,request,id):
        result.objects.get(id=id).delete()
        return redirect('kaoshi:index')
def testfun(request):
    print(type(request))
    # return redirect('index2')
    # return redirect('/school/index2')
    # return redirect('book',33)
    # return redirect('guesioneight','zh',3.3)
    # return redirect('guesioneight',lantype='zh',version=3.3)
    # return redirect('baidu.com')
    # return redirect('http://www.baidu.com')
    # return redirect('http://baidu.com')
    # [命名空间/appname]:路由名字
    # path=reverse('quesioneight',kwargs={'lantype':'zh','version':3.4})
    # indexpath=reverse('index')反向解析
    path=reverse('school2:index')
    print(path)
    return redirect(path)
# def testtem(request):











